//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by mac on 16/3/11.
//  Copyright © 2016年 hw.chow. All rights reserved.
//

import UIKit
import SnapKit

/*
1.protocol : switf中没有 @
2.遵守根协议 <NSObject>
3.如果不遵守NSObjectProtocol<基协议> 就无法使用weak关键字
4.声明可选的协议方法，需要使用 optional 关键字，但是它是OC中的关键字
真要使用的话，可以在声明的协议前加上‘@objc’
*/
 @objc protocol visitorLoginViewDelegate:NSObjectProtocol{

    //协议方法
    //用户将要注册
    optional func userWillRegist()
    //用户将要登录
    func userWillLogin()
    
}

 class VisitorLoginView: UIView {

    /*
    switf属性中，默认的属性关键字就是strong,那么怎么声明一个弱引用呢？
    声明弱引用的属性，需要使用weak
    */

    //声明代理
    weak var visitorViewDelegate: visitorLoginViewDelegate?
    
    //MARK: 按钮的监听事件方法
    @objc private func loginBtnDidClick(){
        
        //使用代理调用协议方法
        visitorViewDelegate?.userWillLogin()
    }
    
    @objc private func registerBtnDidClick(){
        
        visitorViewDelegate?.userWillRegist?()
    }
    
    
    
    //外界调用的重要方法
    func setupUIWithInfo (imageName:String?,tipText:String){
        
        if let img = imageName{
            
            circleIcon.image = UIImage(named: img)
            //隐藏大房子
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
        }else{
            
            //首页
            //调用圈圈的动画效果
            startAnimation()
        }
        tipLabel.text = tipText
    }
    
    //圈圈的动画效果
    func startAnimation(){
        
        //基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        //设置重复次数和旋转度数
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        //持续时间
        base.duration = 10
        
        //设置动画效果完成  /页面失去活跃状态  不移除图层
        base.removedOnCompletion = false
        //添加动画
        circleIcon.layer.addAnimation(base, forKey: nil)
        
    }

    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        
        //添加子视图
        addSubview(circleIcon)
        //解决了遮盖问题
        addSubview(backView)
        
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //snp_makeConstraints 表示添加一条约束
        
        //设置灰度颜色
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //圈圈的背景图
        backView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }

        
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            //垂直居中
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            //垂直居中
            make.centerY.equalTo(circleIcon.snp_centerY)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon)
            make.top.equalTo(circleIcon.snp_bottom).offset(5)
            //设置宽度
            make.width.equalTo(225)
            
            //为了防止页面跳动
            make.height.equalTo(40)
        }
        
        //登录按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
        }
        
        //注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
        }
        
        
        //添加监听事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)

        
    }
    
    //懒加载获取所有的子视图
    
    //圈圈的背景图
    lazy var backView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

    //圈圈
    lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示文案
    lazy var tipLabel:UILabel = {
        
        let l = UILabel()
        
        l.font = UIFont.systemFontOfSize(16)
        l.text = "登陆后，别人评论你的微博登陆后，别人评论你的微博登陆后，别人评论你的微博"
        //设置换行
        l.numberOfLines = 0
        

        l.textColor = UIColor.darkGrayColor()
        l.textAlignment = .Center
        //设置大小
        l.sizeToFit()
        
        return l
        }()
    
    //登陆按钮
    lazy var loginBtn:UIButton = {
        
        let btn = UIButton()
        //设置按钮的文字
        btn.setTitle("登录", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        //切图
        let image = UIImage(named: "common_button_white_disable")!
        let w = Int(image.size.width * 0.5)
        let h = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        
        return btn
        }()
    
    //注册按钮
    lazy var registerBtn:UIButton = {
        
        let btn = UIButton()
        //设置按钮的文字
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        //切图
        let image = UIImage(named: "common_button_white_disable")!
        let w = Int(image.size.width * 0.5)
        let h = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        
        return btn
        }()

}
